from flask import Flask
import time
import json
import requests
from flask_cors import *
from flask import Flask, render_template, redirect, request, url_for , send_file
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)



url = 'https://data.stats.gov.cn/easyquery.htm?cn=C01&zb=A0O0F01&sj=2019'
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
}  # 浏览器代理
r = requests.get(url, headers=headers, verify=False)
Cookie = r.headers['Set-Cookie']
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
    'Cookie': Cookie
}  # 浏览器代理
key = {}  # 参数键值对
key['m'] = 'QueryData'
key['dbcode'] = 'hgnd'
key['rowcode'] = 'zb'
key['colcode'] = 'sj'
key['wds'] = '[]'
key['dfwds'] = '[{"wdcode":"sj","valuecode":"LAST13"}]'
key['k1'] = int(round(time.time() * 1000))
r = requests.get(url, headers=headers, params=key, verify=False)
js = json.loads(r.text)
# print(js)


length = len(js["returndata"]["datanodes"])
list_2 = js['returndata']['wdnodes'][0]['nodes']
print("list_2:",list_2)
print(len(list_2))
print(list_2[0]['cname'])
# name__list = []
list_need = []
num = 0
print("year:",js["returndata"]["datanodes"][12]["wds"][1]["valuecode"])
print("value_2007:",js["returndata"]["datanodes"][12]["data"]["strdata"])
print("year:",js["returndata"]["datanodes"][5]["wds"][1]["valuecode"])
print("value_2014:",js["returndata"]["datanodes"][3*13+5]["data"]["strdata"])


for i in range(len(list_2)):
    # datas_need = ("{'name': '%s','%s': '%s'}" %( list_2[0]['cname'][:-3],js["returndata"]["datanodes"][i]["wds"][1]["valuecode"],js["returndata"]["datanodes"][i]["data"]["strdata"]))
    name = list_2[i]['cname'][:-3]
    # num = num +13
    # print(name)
    for i_2 in range(13):
        name = name
        # year = js["returndata"]["datanodes"][i_2]["wds"][1]["valuecode"]
        # value = js["returndata"]["datanodes"][i]["data"]["strdata"]
        year = js["returndata"]["datanodes"][i_2]["wds"][1]["valuecode"]
        value = js["returndata"]["datanodes"][(i * 13) + i_2]["data"]["strdata"]

        # datas_need = ("{'name': '%s','%s': '%s'}" % (name, year, value))
        datas_need = ("{'name': '%s','%s': '%s'}" % (name, year, value))
        list_need.append(datas_need)
print(list_need)






